<template>
    <div class="user-container">
        <!-- 用户信息卡片 -->
        <div class="user-card">
            <div class="user-header">
                <div class="avatar">
                    {{ userInfo.username ? userInfo.username.charAt(0).toUpperCase() : '?' }}
                </div>
                <div class="user-info">
                    <h2 class="username">{{ userInfo.username || '未登录' }}</h2>
                    <p class="user-id">ID: {{ userInfo.id || '-' }}</p>
                </div>
            </div>
        </div>

        <!-- 功能菜单 -->
        <div class="menu-list">
            <div class="menu-group">
                <div class="menu-item" @click="goToMyBikes">
                    <i class="menu-icon">🚲</i>
                    <span class="menu-text">我的组装</span>
                    <i class="arrow">→</i>
                </div>
                <div class="menu-item" @click="goToCart">
                    <i class="menu-icon">🛒</i>
                    <span class="menu-text">购物车</span>
                    <i class="arrow">→</i>
                </div>
                <div class="menu-item" @click="goToFavorites">
                    <i class="menu-icon">⭐</i>
                    <span class="menu-text">我的收藏</span>
                    <i class="arrow">→</i>
                </div>
            </div>

            <div class="menu-group">
                <div class="menu-item" @click="goToSettings">
                    <i class="menu-icon">⚙️</i>
                    <span class="menu-text">设置</span>
                    <i class="arrow">→</i>
                </div>
                <div class="menu-item" @click="goToHelp">
                    <i class="menu-icon">❓</i>
                    <span class="menu-text">帮助与反馈</span>
                    <i class="arrow">→</i>
                </div>
            </div>

            <!-- 退出登录按钮 -->
            <div class="logout-btn" @click="handleLogout" v-if="userInfo.username">
                退出登录
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getUserInfo } from '@/api/index.js'
import { ElMessage } from 'element-plus'

const router = useRouter()
const userInfo = ref({})

// 获取用户信息
const loadUserInfo = async () => {
    try {
        const res = await getUserInfo()
        userInfo.value = res.data
    } catch (error) {
        ElMessage.error('获取用户信息失败')
    }
}

// 页面跳转方法
const goToMyBikes = () => router.push('/my-bikes')
const goToCart = () => router.push('/cart')
const goToFavorites = () => router.push('/favorites')
const goToSettings = () => router.push('/settings')
const goToHelp = () => router.push('/help')

// 退出登录
const handleLogout = async () => {
    try {
        localStorage.removeItem('token')
        sessionStorage.removeItem('token')
        localStorage.removeItem('userInfo')
        ElMessage.success('退出成功')
        router.push('/login')
    } catch (error) {
        ElMessage.error('退出失败')
    }
}

onMounted(() => {
    loadUserInfo()
})
</script>

<style lang="css" scoped>
.user-container {
    padding-top: calc(56px + env(safe-area-inset-top));
    min-height: 100vh;
    background: #f5f7fa;
}

.user-card {
    background: #409EFF;
    padding: 20px;
    color: white;
}

.user-header {
    display: flex;
    align-items: center;
    gap: 16px;
}

.avatar {
    width: 64px;
    height: 64px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    font-weight: 600;
    border: 2px solid rgba(255, 255, 255, 0.5);
}

.user-info {
    flex: 1;
}

.username {
    font-size: 20px;
    font-weight: 600;
    margin-bottom: 4px;
}

.user-id {
    font-size: 14px;
    opacity: 0.8;
}

.menu-list {
    padding: 16px;
}

.menu-group {
    background: white;
    border-radius: 12px;
    margin-bottom: 16px;
    overflow: hidden;
}

.menu-item {
    display: flex;
    align-items: center;
    padding: 16px;
    border-bottom: 1px solid #f0f0f0;
    cursor: pointer;
}

.menu-item:last-child {
    border-bottom: none;
}

.menu-item:active {
    background: #f5f5f5;
}

.menu-icon {
    font-size: 20px;
    margin-right: 12px;
}

.menu-text {
    flex: 1;
    font-size: 16px;
    color: #333;
}

.arrow {
    color: #999;
    font-size: 16px;
}

.logout-btn {
    margin-top: 32px;
    background: white;
    color: #ff4d4f;
    padding: 16px;
    text-align: center;
    border-radius: 12px;
    font-size: 16px;
    font-weight: 500;
    cursor: pointer;
}

.logout-btn:active {
    background: #f5f5f5;
}

/* 适配深色模式 */
@media (prefers-color-scheme: dark) {
    .user-container {
        background: #1a1a1a;
    }

    .menu-group {
        background: #2a2a2a;
    }

    .menu-item {
        border-bottom-color: #333;
    }

    .menu-text {
        color: #fff;
    }

    .menu-item:active {
        background: #333;
    }

    .logout-btn {
        background: #2a2a2a;
    }

    .logout-btn:active {
        background: #333;
    }
}
</style>